/**
 * SpringMVC restful 风格
 * 合作伙伴控制器，控制关于合作伙伴的一些业务逻辑操作
 * @author yanwl
 * @date 2014-08-20
 */
package com.chnbst.bstyun.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.CompanyIndustry;
import com.chnbst.bstyun.model.pojo.CompanyScale;
import com.chnbst.bstyun.model.pojo.Menu;
import com.chnbst.bstyun.model.pojo.Partner;
import com.chnbst.bstyun.model.pojo.Province;
import com.chnbst.bstyun.model.pojo.Supplier;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.service.CompanyIndustryService;
import com.chnbst.bstyun.service.CompanyScaleService;
import com.chnbst.bstyun.service.MenuService;
import com.chnbst.bstyun.service.PartnerService;
import com.chnbst.bstyun.service.PlaceService;
import com.chnbst.bstyun.service.SupplierService;
import com.chnbst.bstyun.service.UserService;

@Controller
@RequestMapping("/partner")
public class PartnerController {
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private MenuService menuService;
	@Autowired
	private PartnerService partnerService;
	@Autowired
	private UserService userService;
	@Autowired
	private SupplierService supplierService;
	@Autowired 
	private CompanyIndustryService companyIndustryService;
	@Autowired
	private CompanyScaleService companyScaleService;
	@Autowired
	private PlaceService placeService;
	
	/**
	 * http://xxx.com/partner
	 * get请求 返回合作伙伴首页
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView partnerIndex(HttpServletRequest request){
		List<Partner> partnerList = partnerService.selectAllPartners();
		List<Menu> menus = menuService.selectMenus();
		Partner partner = partnerService.selectByPartnerId(1);
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.addObject("partnerList", partnerList);
		mav.addObject("partner",partner);
		mav.setViewName("cooperater/joinus");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner
	 * get请求 返回合作伙伴首页
	 */
	@RequestMapping(value="/{partnerId}",method=RequestMethod.GET)
	public ModelAndView cooperater(@PathVariable Integer partnerId,HttpServletRequest request){
		List<Partner> partnerList = partnerService.selectAllPartners();
		List<Menu> menus = menuService.selectMenus();
		Partner partner = partnerService.selectByPartnerId(partnerId);
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.addObject("partnerList", partnerList);
		mav.addObject("partner",partner);
		mav.addObject("returnUrl", request.getRequestURI());
		mav.setViewName("cooperater/joinus");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner
	 * get请求 返回合作伙伴首页
	 */
	@RequestMapping(value="/checkIsApply",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> checkIsApply(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String, Object>();
		
		//查询当前登陆者 的信息
		User user = (User) request.getSession().getAttribute("userInfo");
		if(user.getSupplierId() == null && !"0".equals(user.getIsYun())) {
			map.put("isApply", false);
		}else {
			map.put("isApply", true);
			if("0".equals(user.getIsYun())) {
				map.put("warn", "管理员账号不能申请代理商哦！！");
			}else {
				Supplier supplier = supplierService.selectSupplierBySupplierId(user.getSupplierId());
				if(supplier.getApplyState() == 0) {
					map.put("warn", "您的申请已经提交，正在审核中，请耐心等待1-3个工作日！！");
				}else if(supplier.getApplyState() == 1 && supplier.getIsValid() == 1) {
					map.put("warn", "您已经申请通过了，不能再申请了！！");
				}else if(supplier.getApplyState() == 1 && supplier.getIsValid() == 0) {
					map.put("warn", "您的代理商账户已经被停用，不能再申请了，请联系客服！！");
				}else if(supplier.getApplyState() == 2) {
					map.put("isApply", false);
				}
			}
		}
		
		return map;
	}
	
	/**
	 * http://xxx.com/partner/apply
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到伙伴申请页面
	 */
	@RequestMapping(value="/apply",method=RequestMethod.GET)
	public ModelAndView apply(HttpServletRequest request){
		
		/**
		 * 公司所属行业
		 */
		List<CompanyIndustry> companyIndustries = companyIndustryService.selectCompanyIndustries();
		/**
		 * 公司规模
		 */
		List<CompanyScale> companyScales = companyScaleService.selectCompanyScales();
		
		/**
		 * 地区联动
		 */
		List<Province> provinceList = placeService.getProvinceList();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("companyIndustries", companyIndustries);
		mav.addObject("companyScales", companyScales);
		mav.addObject("provinceList", provinceList);
		mav.addObject("applyType", request.getParameter("applyType"));
		mav.addObject("returnUrl", request.getRequestURI());
		mav.setViewName("cooperater/hbsq");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/apply/commit
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到伙伴申请页面
	 */
	@RequestMapping(value="/apply/commit",method=RequestMethod.POST)
	@ResponseBody
	public boolean commit(Supplier supplier,HttpServletRequest request){
		
		//查询当前登陆者 的信息
		User user = (User) request.getSession().getAttribute("userInfo");
		
		boolean b = false;
		boolean c = false;
		boolean a = false;
		
		//设置申请状态为提交，并插入到代理商信息表中
		supplier.setApplyState(0);
		supplier.setAddTime(new Date());
		supplier.setUserId(user.getUserId());
		supplier.setIsValid(0);
		b = supplierService.insertSupplier(supplier);
		
		//如果插入成功，将新增代理商信息的主键 放入user表中
		if(b) {
			user.setSupplierId(supplier.getSupplierId());
			c = userService.updateUser(user);
		}
		
		if(b && c) {
			a = true;
		}
		return a;
	}
	
	/**
	 * http://xxx.com/partner/cooperater/qrtj
	 * get请求 返回代理商申请提交页面
	 */
	@RequestMapping(value="/cooperater/qrtj",method=RequestMethod.GET)
	public ModelAndView qrtj(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("cooperater/qrtj");
		return mav;
	}
	
	
	/**
	 * http://xxx.com/partner/developer
	 * get请求 返回软件开发商页面
	 */
	@RequestMapping(value="/developer",method=RequestMethod.GET)
	public ModelAndView partner(){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/partner");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/developer/kfsxq
	 * get请求 返回开发商详情页面
	 */
	@RequestMapping(value="/developer/kfsxq",method=RequestMethod.GET)
	public ModelAndView kfsxq(){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/kfsxq");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/developer/kfsxq/fsyj
	 * get请求 返回给开发商发送邮件页面
	 */
	@RequestMapping(value="/developer/kfsxq/fsyj",method=RequestMethod.GET)
	public ModelAndView fsyj(){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/fsyj");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/qddls
	 * get请求 返回渠道代理商页面
	 */
	@RequestMapping(value="/qddls",method=RequestMethod.GET)
	public ModelAndView qddls(){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		/**
		 * 地区联动相关====================
		 */
		
		List<Province> provinceList = placeService.getProvinceList();
		mav.addObject("provinceList",provinceList);
		/**
		 * 地区联动相关====================
		 */
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/qddls");
		return mav;
	}
}
